package com.swak.algs;

import java.util.Stack;

import com.swak.algs.node.TreeNode;

/**
 * 符合条件的路径
 * 
 * @author lifeng
 */
public class Code14 {

	static Stack<TreeNode> nodes = new Stack<>();

	static void process(TreeNode head, int preSum, int sum) {
		nodes.add(head);
		if ((head.left == null) & (head.right == null)) {
			if (preSum + head.val == sum) {
				printQueue();
			}
		}
		if (head.left != null) {
			process(head.left, preSum + head.val, sum);
		}
		if (head.right != null) {
			process(head.right, preSum + head.val, sum);
		}
		nodes.pop();
	}

	static void printQueue() {
		nodes.forEach(node -> System.out.print(node.val + " "));
		System.out.println();
	}

	public static void main(String[] args) {
		TreeNode root = TreeNode.newNoMirrorTree();
		process(root, 0, 17);
	}
}